package com.eclink.asset.workflow.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eclink.asset.domain.AuditProcess;
import com.eclink.asset.domain.AuditConfigNode;
import com.eclink.asset.mapper.AssetAuditConfigMapper;
import com.eclink.asset.mapper.AuditConfigNodeMapper;
import com.eclink.asset.pojo.query.AssetAuditConfigQuery;
import com.eclink.asset.workflow.enums.AuditProcessStatusEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 【请填写功能名称】Service业务层处理
 *
 *
 * @author zcc
 */
@Service
public class AuditConfigService {

    @Resource
    private AssetAuditConfigMapper assetAuditConfigMapper;

    @Resource
    private AuditConfigNodeMapper assetAuditConfigNodeMapper;

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    public AuditProcess selectById(Integer id) {
        return assetAuditConfigMapper.selectById(id);
    }

    public AuditProcess selectByOrderType(String orderType) {
        QueryWrapper<AuditProcess> qw = new QueryWrapper<>();
        qw.eq("order_type", orderType);
        return assetAuditConfigMapper.selectOne(qw);
    }

    public List<AuditConfigNode> getNodes(Long configId) {
        QueryWrapper<AuditConfigNode> qw = new QueryWrapper<>();
        qw.eq("config_id", configId);
        return assetAuditConfigNodeMapper.selectList(qw);
    }


    /**
     * 查询【请填写功能名称】列表
     *
     * @param query 查询条件
     * @param page 分页条件
     * @return 【请填写功能名称】
     */
    public IPage<AuditProcess> selectList(AssetAuditConfigQuery query, Pageable page, Integer configType) {

        QueryWrapper<AuditProcess> qw = new QueryWrapper<>();
        qw.eq("del_flag",0);
        String configNameLike = query.getConfigNameLike();
        if (!StringUtils.isEmpty(configNameLike)) {
            qw.like("config_name", configNameLike);
        }
        String configDesc = query.getConfigDesc();
        if (!StringUtils.isEmpty(configDesc)) {
            qw.eq("config_desc", configDesc);
        }

        if (configType != null) {
            qw.eq("config_type", configType);
        }
        String orderType = query.getOrderType();
        if (!StringUtils.isEmpty(orderType)) {
            qw.eq("order_type", orderType);
        }
        Integer status = query.getStatus();
        if (status != null) {
            qw.eq("status", status);
        }
        Integer createUserId = query.getCreateUserId();
        if (createUserId != null) {
            qw.eq("create_user_id", createUserId);
        }
        Integer updateUserId = query.getUpdateUserId();
        if (updateUserId != null) {
            qw.eq("update_user_id", updateUserId);
        }
        return assetAuditConfigMapper.selectPage(new Page<>(page.getPageNumber(), page.getPageSize()), qw);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param auditProcess 【请填写功能名称】
     * @return 结果
     */
    public int insert(AuditProcess auditProcess) {
        auditProcess.setDelFlag(0);
        auditProcess.setCreateTime(LocalDateTime.now());
        return assetAuditConfigMapper.insert(auditProcess);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param auditProcess 【请填写功能名称】
     * @return 结果
     */
    public int update(AuditProcess auditProcess) {
        return assetAuditConfigMapper.updateById(auditProcess);
    }

    public int getNodeCount(Long id) {
        LambdaQueryWrapper<AuditConfigNode> qw = new LambdaQueryWrapper<>();
        qw.eq(AuditConfigNode::getConfigId, id);
        return assetAuditConfigNodeMapper.selectCount(qw);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param auditProcess 【请填写功能名称】
     * @return 结果
     */
    public int updateStatus(AuditProcess auditProcess) {

        Long id = auditProcess.getId();
        Integer status = auditProcess.getStatus();
        // 在启动流程前，检查流程是否有节点
        if (status == AuditProcessStatusEnum.ACTIVE.getValue()) {
            List<AuditConfigNode> nodes = this.getNodes(id);
            if (nodes == null || nodes.isEmpty()) {
                return -1;
            }
        }

        LambdaUpdateWrapper<AuditProcess> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(AuditProcess::getId, id).set(AuditProcess::getStatus, status);
        return assetAuditConfigMapper.update(null, updateWrapper);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    public int deleteByIds(Long[] ids) {
        for(Long id : ids) {
            assetAuditConfigMapper.deleteById(id);
        }
        return 1;
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    public int deleteById(Long id) {
        return assetAuditConfigMapper.deleteById(id);
    }
}
